package hiatao.puzzle.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.mark.imageloader.PicSelActivity;

import java.util.ArrayList;
import java.util.List;

import haitao.puzzle.R;
import hiatao.puzzle.activity.PuzzleGameActivity;
import hiatao.puzzle.adapter.StaggeredAdapter;
import hiatao.puzzle.cascade.CascadeListView;
import hiatao.puzzle.cascade.CascadeListView.IXListViewListener;
import hiatao.puzzle.cascade.internal.PLA_AdapterView;
import hiatao.puzzle.cascade.internal.PLA_AdapterView.OnItemClickListener;
import hiatao.puzzle.config.URLS;
import hiatao.puzzle.entity.PuzzleInfo;
import hiatao.puzzle.utils.NameUtils;
import hiatao.puzzle.utils.Tools;

/**
 * 统计部分
 * 
 * @author yzx
 * 
 */
@SuppressLint("ValidFragment")
public class HomeFragment extends Fragment implements IXListViewListener {
	
	private View rootView;
	private ImageView cameraIv;
	private ImageView arrowView;
    private CascadeListView cListView = null;
    private StaggeredAdapter mAdapter = null;
    List<PuzzleInfo> list = new ArrayList<PuzzleInfo>();
    private TextView titleTv;
    private PopupWindow popu;
    String titles[] = {"热门","附近","关注"};
 
    int index =0;
    int page =1;
    //RefeshClear
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_home, container, false);
		findView();
		init();
		return rootView;
	}

	private void findView() {

		cameraIv =  (ImageView)rootView.findViewById(R.id.cameraIv);
		cameraIv.setColorFilter(getResources().getColor(R.color.white));
		cListView = (CascadeListView)  rootView.findViewById(R.id.list);
        cListView.setPullLoadEnable(true);
        cListView.setPullRefreshEnable(false);
        cListView.setXListViewListener(this);
        mAdapter = new StaggeredAdapter(getActivity());
        cListView.setAdapter(mAdapter);
        mAdapter.addItemLast(getTestData());
	}
    
	
	private List<PuzzleInfo> getTestData(){
		list = new ArrayList<PuzzleInfo>();
	  //  int start = mAdapter.getCount();
	   // 
		for(int i=0;i<7;i++){
			//i=0  page = 1 sum =1  (page-1)*7+1+i
			//i=0  page = 2 sum =8
			//i=0  page =3 sum =15
			int count= (page-1)*7+1+i+index;
			PuzzleInfo info= new  PuzzleInfo();
			info.setPicture(URLS.BASE_PICTURE+(count +".jpg"));
			info.setUserHead(URLS.BASE_USER+(count+".png"));
			info.setJoinNum(Tools.getRandom(0, 100));
			info.setName(NameUtils.getNames().get(count-1).getName());
			info.setIntro(NameUtils.getNames().get(count-1).getTitle());
			info.setLikeNum(Tools.getRandom(0, 100));
			list.add(info);
		}
		return list;
	}
	
	private void init(){
        
		cameraIv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//ActionSheet.showSheet(getActivity(),HomeFragment.this, HomeFragment.this);
				getActivity().startActivity(new Intent().setClass(getActivity(), PicSelActivity.class));
			}
		});
		cListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(PLA_AdapterView<?> parent, View view,
					int position, long id) {
					Intent intent = new Intent();
					intent.setClass(getActivity(), PuzzleGameActivity.class);
					PuzzleInfo  p =(PuzzleInfo) mAdapter.getItem(position-1);
					intent.putExtra("HEADURL",p.getUserHead());
					intent.putExtra("URL",p.getPicture());
					intent.putExtra("NAME",p.getName());
					intent.putExtra("INTRO",p.getIntro());
					startActivity(intent);
			}
			
		});
		titleTv = (TextView) rootView.findViewById(R.id.title);
		arrowView = (ImageView) rootView.findViewById(R.id.down);
		titleTv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showPop();
			}
		});
		arrowView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showPop();
			}
		});
		
	}
	protected void showPop() {
		
		TextView item1 = null;
		TextView item2 = null;
		//if (popu == null) {
			View popuView=null;
			popuView = View.inflate(getActivity(), R.layout.ppw_choose, null);;
			popu = new PopupWindow(popuView,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			popu.setBackgroundDrawable(new BitmapDrawable());
			popu.setTouchable(true);
			popu.setFocusable(true);
			item1 = (TextView) popuView.findViewById(R.id.item1);
			item2 = (TextView) popuView.findViewById(R.id.item2);
		//}
		String text = titleTv.getText().toString();
		int i =0;
		for(String str:titles){
			
			if(!str.equals(text)){
				if(i==0){
					item1.setText(str);
					i++;
				}else{
					item2.setText(str);
					i++;
				}
			}
		}
		final String text1= item1.getText().toString();
		final String text2= item2.getText().toString();
		item1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			titleTv.setText(text1);
			popu.dismiss();
			change();
			}
		});
		item2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				titleTv.setText(text2);
				popu.dismiss();
				change();
			}
		});
		popu.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				arrowView.setImageResource(R.drawable.arrow_down_sel);
			}
		});
		arrowView.setImageResource(R.drawable.arrow_up_sel);
		popu.showAsDropDown(titleTv,0, 0);
	}

	private void change(){
		    //RefeshClear"热门","附近","关注"
		 if(titleTv.getText().equals("热门")){
			 index=0;
		 }else  if(titleTv.getText().equals("附近")){
			 index=14;
		 }else{
			 index=28;
		 }
		// mAdapter.clear();
		 page= 1;
		 cListView.setPullLoadEnable(true);
		 mAdapter.RefeshClear(getTestData());
		 
	}
	public View getRootView() {
		return rootView;
	}

	@Override
	public void onRefresh() {
		
	}

	@Override
	public void onLoadMore() {
    	if(page<2){
    		new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					page++;
		    		mAdapter.addItemLast(getTestData());
		    		cListView.stopLoadMore();
		    		if(page==2){
		    			cListView.setPullLoadEnable(false);
		    		}
				}
			}, 3000);
    		
    	} 
    	else{
    		cListView.stopLoadMore();
    		cListView.setPullLoadEnable(false);
    	}
    }

/*	@Override
	public void onCancel(DialogInterface dialog) {
		
		
	}

	@Override
	public void onClick(int whichButton) {
		// TODO Auto-generated method stub
		if (whichButton==1) {
			Tools.getToPicture(getActivity());
		}else if(whichButton==2){
			//getActivity().startActivity(new Intent().setClass(getActivity(), CameraActivity.class));
			getActivity().startActivity(new Intent().setClass(getActivity(), PicSelActivity.class));
			
		}
	}*/

}
